<template>
  <div>
    <div id="id">
      <h1>NUI-ADMIN</h1>
      <el-form :model="login" class="demo-form-inline">
        <el-form-item>
          <el-input v-model="login.user" placeholder="用户名"></el-input>
        </el-form-item>
        <el-form-item>
          <el-input
            v-model="login.pass"
            placeholder="密码"
            type="password"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" style="width: 100%"
            >登录</el-button
          >
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      login: {
        user: "",
        pass: "",
      },
    };
  },
  methods: {
    //   登录
    onSubmit() {
      if (this.login.user.length == 0 || this.login.pass.length == 0) {
        this.$message({
          message: "账号或密码不能为空",
          type: "warning",
        });
        return;
      }
      this.$axios
        .post("http://ceshi5.dishait.cn/admin/login", {
          username: this.login.user,
          password: this.login.pass,
        })
        .then((res) => {
          console.log(res);
          if (res.data.msg == "ok") {
            this.$message({
              message: "登录成功",
              type: "success",
            });
            localStorage.setItem("token", res.data.data.token);
            var date = new Date().getTime()+1000*60*60;
            localStorage.setItem("time", date);
            this.$router.push("/home");
          }
        })
        .catch(() => {
          this.$message.error("账号或密码错误");
        });
    },
  },
};
</script>
<style lang="scss" scoped>
#id {
  width: 300px;
  padding: 0 30px 20px 30px;
  border: 1px solid gainsboro;
  margin: 100px auto;
  h1 {
    display: flex;
    justify-content: center;
  }
}
</style>